*** Tables: Balance Tables
*** Data: Employee Data Set

**# Balance Tables with all employees #1

clear all 

*** Dataset: 
use "$data\4_individual_ano_vars.dta", clear

keep if wave==1
keep if employer==0

*** Define variables for balance check
local balvars age male married_cohabiting educ_none educ_prim educ_sec educ_tert ///
    supervisor supervised experience_sector tenure_years ///
    wrcon sosec mwage_mio aminwage empquality_ind hoursworked satisfied training_any


*** Prepare labels
label var age "Age"
label var male "Male"
label var married_cohabiting "Married or cohabiting"
label var educ_none "Education: none"
label var educ_prim "Education: primary"
label var educ_sec "Education: secondary"
label var educ_tert "Education: tertiary"
label var supervisor "Supervisory role"
label var supervised "Staff supervised"
label var experience_sector "Experience in sector (years)"
label var tenure_years "Tenure (years)"
label var wrcon "Written contract"
label var sosec "Social security"
label var mwage_mio "Monthly wage (Mio)"
label var aminwage "At least min wage"
label var empquality_ind "Formality Index"
label var hoursworked "Weekly hours"
label var satisfied "Satisfied"
label var training_any "Training Participation"

*** Create LaTeX table directly
file open table using "$results\01_tables\Table_1_balance_baseline.tex", write replace
file write table "\begin{tabular}{lllllll} \hline \hline" _n
file write table "                             & \multicolumn{2}{l}{\textbf{Treatment}}    & \multicolumn{2}{l}{\textbf{Control}}   & \multicolumn{2}{l}{\textbf{Orthogonality}}   \\" _n
file write table "                               & \multicolumn{2}{l}{(1)}          & \multicolumn{2}{l}{\textbf{(2)}}       & \multicolumn{2}{l}{\textbf{Mean (1)-(2)}}    \\" _n
file write table "                               & \textbf{N}              & \textbf{Mean}            & \textbf{N}            & \textbf{Mean}           & \textbf{Difference}         & \textbf{p-value}      \\ \hline \\" _n
file write table "\textbf{Individual characteristics}   &                &                 &              &                &                    &                \\ \hline" _n

*** Calculate statistics and write rows
foreach var of varlist `balvars' {
    qui mean `var', over(assignment)
    local nt = e(_N)[1,2]
    local nc = e(_N)[1,1]
    local mt : display %5.2f e(b)[1,2]
    local mc : display %5.2f e(b)[1,1]
    local diff : display %5.2f e(b)[1,2]-e(b)[1,1]
    qui ttest `var', by(assignment)
    local p : display %5.2f r(p)
    
    local label : variable label `var'
    
    * Add section headers based on variable position
    if "`var'"=="supervisor" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Work situation}                &                &                 &              &                &                    &                \\ \hline" _n
    }
    if "`var'"=="wrcon" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Outcomes}                       &                &                 &              &                &                    &                \\ \hline" _n
    }
    
    file write table "`label' &         `nt'&       `mt'&         `nc'&       `mc'&       `diff'&        `p'\\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n
file close table


**# Balance Tables with endline employees #1

clear all 
*** Dataset: 
use "$data\4_individual_ano_vars.dta", clear
* First, set seed for reproducibility
set seed 12345

merge 1:1 id_indiv wave using "$data\4_data_for_balance.dta", keepusing(used_w3_w1) nogen

keep if used_w3_w1 == 1 & wave == 1

*** Define variables for balance check
local balvars age male married_cohabiting educ_none educ_prim educ_sec educ_tert ///
    supervisor supervised experience_sector tenure_years ///
    wrcon sosec mwage_mio aminwage empquality_ind hoursworked satisfied training_any

*** Prepare labels
label var age "Age"
label var male "Male"
label var married_cohabiting "Married or cohabiting"
label var educ_none "Education: none"
label var educ_prim "Education: primary"
label var educ_sec "Education: secondary"
label var educ_tert "Education: tertiary"
label var supervisor "Supervisory role"
label var supervised "Staff supervised"
label var experience_sector "Experience in sector (years)"
label var tenure_years "Tenure (years)"
label var wrcon "Written contract"
label var sosec "Social security"
label var mwage_mio "Monthly wage (Mio)"
label var aminwage "At least min wage"
label var empquality_ind "Formality Index"
label var hoursworked "Weekly hours"
label var satisfied "Satisfied"
label var training_any "Training Participation"

*** Create LaTeX table directly
file open table using "$results\01_tables\Table_S4_balance_endline.tex", write replace
file write table "\begin{tabular}{lllllll} \hline \hline" _n
file write table "                             & \multicolumn{2}{l}{\textbf{Treatment}}    & \multicolumn{2}{l}{\textbf{Control}}   & \multicolumn{2}{l}{\textbf{Orthogonality}}   \\" _n
file write table "                               & \multicolumn{2}{l}{(1)}          & \multicolumn{2}{l}{\textbf{(2)}}       & \multicolumn{2}{l}{\textbf{Mean (1)-(2)}}    \\" _n
file write table "                               & \textbf{N}              & \textbf{Mean}            & \textbf{N}            & \textbf{Mean}           & \textbf{Difference}         & \textbf{p-value}      \\ \hline \\" _n
file write table "\textbf{Individual characteristics}   &                &                 &              &                &                    &                \\ \hline" _n

*** Calculate statistics and write rows
foreach var of varlist `balvars' {
    qui mean `var', over(assignment)
    local nt = e(_N)[1,2]
    local nc = e(_N)[1,1]
    local mt : display %5.2f e(b)[1,2]
    local mc : display %5.2f e(b)[1,1]
    local diff : display %5.2f e(b)[1,2]-e(b)[1,1]
    qui ttest `var', by(assignment)
    local p : display %5.2f r(p)
    
    local label : variable label `var'
    
    * Add section headers based on variable position
    if "`var'"=="supervisor" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Work situation}                &                &                 &              &                &                    &                \\ \hline" _n
    }
    if "`var'"=="wrcon" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Outcomes}                       &                &                 &              &                &                    &                \\ \hline" _n
    }
    
    file write table "`label' &         `nt'&       `mt'&         `nc'&       `mc'&       `diff'&        `p'\\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n
file close table


**# Balance Tables Hiring #1

clear all 
*** Dataset: 
use "$data\4_individual_ano_reg_3.dta", clear

keep if employer==0
	replace status = 0 if status == 2
	keep if status == 0

*** Define variables for balance check
local balvars age male educ_none_base educ_prim_base educ_sec_base educ_tert_base ///
    supervisor supervised experience_sector 

*** Prepare labels
label var age "Age"
label var male "Male"
label var educ_none_base "Education: none"
label var educ_prim_base "Education: primary"
label var educ_sec_base "Education: secondary"
label var educ_tert_base "Education: tertiary"
label var supervisor "Supervisory role"
label var supervised "Staff supervised"
label var experience_sector "Experience in sector (years)"


*** Create LaTeX table directly
file open table using "$results\01_tables\Table_S5_new_hires.tex", write replace
file write table "\begin{tabular}{lllllll} \hline \hline" _n
file write table "                             & \multicolumn{2}{l}{\textbf{Treatment}}    & \multicolumn{2}{l}{\textbf{Control}}   & \multicolumn{2}{l}{\textbf{Orthogonality}}   \\" _n
file write table "                               & \multicolumn{2}{l}{(1)}          & \multicolumn{2}{l}{\textbf{(2)}}       & \multicolumn{2}{l}{\textbf{Mean (1)-(2)}}    \\" _n
file write table "                               & \textbf{N}              & \textbf{Mean}            & \textbf{N}            & \textbf{Mean}           & \textbf{Difference}         & \textbf{p-value}      \\ \hline \\" _n
file write table "\textbf{Individual characteristics}   &                &                 &              &                &                    &                \\ \hline" _n

*** Calculate statistics and write rows
foreach var of varlist `balvars' {
    qui mean `var', over(assignment)
    local nt = e(_N)[1,2]
    local nc = e(_N)[1,1]
    local mt : display %5.2f e(b)[1,2]
    local mc : display %5.2f e(b)[1,1]
    local diff : display %5.2f e(b)[1,2]-e(b)[1,1]
    qui ttest `var', by(assignment)
    local p : display %5.2f r(p)
    
    local label : variable label `var'
    
    * Add section headers based on variable position
    if "`var'"=="supervisor" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Work situation}                &                &                 &              &                &                    &                \\ \hline" _n
    }
    
    file write table "`label' &         `nt'&       `mt'&         `nc'&       `mc'&       `diff'&        `p'\\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n
file close table